<!DOCTYPE html>
<html lang="en">
    <head>
  <!-- 元数据 -->
  <meta charset="utf-8">
  <link rel="icon" href="https://gitee.com/cuishikang/tmp/raw/master/img/boke.png">
  <title>axios封装通用的api接口方法 | 小崔的博客</title>
  <meta name="author" content="cuishikang" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="robots" content="index,follow" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta name="format-detection" content="telphone=no, email=no" />
  
    <meta name="keywords" content="Vue, axios" />
  
  <meta name="description" content="::: tip 介绍二次封装axios 实现api接口直接调用:::">
<meta property="og:type" content="article">
<meta property="og:title" content="axios封装通用的api接口方法">
<meta property="og:url" content="https://cuishikang.gitee.io/2022/01/13/axios/1_axios_API/index.html">
<meta property="og:site_name" content="小崔的博客">
<meta property="og:description" content="::: tip 介绍二次封装axios 实现api接口直接调用:::">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/cuishikang/tmp/raw/master/img/boke.png">
<meta property="article:published_time" content="2022-01-12T16:00:00.000Z">
<meta property="article:modified_time" content="2022-02-19T10:22:58.553Z">
<meta property="article:author" content="cuishikang">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="axios">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/cuishikang/tmp/raw/master/img/boke.png">
<meta name="twitter:site" content="@null">
  
  <!-- 站点验证相关 -->
  
    
    
    
  
  <!-- 样式表文件 -->
  <link rel="stylesheet" id="kratos-css" href="/css/kratosr.min.css" type="text/css" media="all">
  
    <link rel="stylesheet" id="highlight-css" href="/css/highlight/night-eighties.min.css" type="text/css" media="all">
  
  
  <link rel="stylesheet" id="fontawe-css" href="https://unpkg.com/font-awesome@4.7.0/css/font-awesome.min.css" type="text/css" media="all">
  <link rel="stylesheet" id="nprogress-css" href="https://unpkg.com/nprogress@0.2.0/nprogress.css" type="text/css" media="all">
  
  
    <link rel="stylesheet" href="https://unpkg.com/aplayer@1.10.1/dist/APlayer.min.css">
  
  
    <link rel="stylesheet" href="https://unpkg.com/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">
  
  
    <link rel="stylesheet" id="darkmode-css" href="/css/kr-dark.min.css" type="text/css" media="all">
  
  <!-- 不得不预先加载的一些JS文件 -->
  <script src="https://unpkg.com/jquery@3.6.0/dist/jquery.min.js"></script>
  
    <script src="https://unpkg.com/qrcode_js@1.0.0/qrcode.min.js"></script>
  
  
  <style>
    
      .kratos-cover.kratos-cover-2 {
        background-image: url('/images/banner.webp');
      }
    
    
      @media(min-width:768px) {
        body.custom-background {
          background-image: url('/images/bg.webp');
        }
      }
    
  </style>
  
<meta name="generator" content="Hexo 6.0.0"></head>


    <body class="custom-background">
        <div id="kratos-wrapper">
    <div id="kratos-page">
        <div id="kratos-header">
            <header id="kratos-desktop-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="nav-header">
                        <nav id="kratos-menu-wrap">
                            <ul id="kratos-primary-menu" class="sf-menu">
                                
                                    
                                        <li><a href="/"><i class="fa fa-home"></i>首页</a></li>
                                    
                                
                                    
                                        <li><a href="/archives/"><i class="fa fa-file"></i>档案馆</a></li>
                                    
                                
                                    
                                        <li><a href="/friends/"><i class="fa fa-paw"></i>好伙伴</a></li>
                                    
                                
                                    
                                        <li>
                                            <a><i class="fa fa-link"></i>关于本人</a>
                                            <ul class="sub-menu">
                                                
                                                    
                                                
                                                    
                                                        <li><a target="_blank" rel="noopener" href="https://gitee.com/cuishikang"><i class="fa fa-git"></i>码云</a></li>
                                                    
                                                
                                                    
                                                        <li><a target="_blank" rel="noopener" href="https://gitee.com/cuishikang/tmp/raw/master/img/2201645358312_.pic.jpg"><i class="fa fa-weixin"></i>微信</a></li>
                                                    
                                                
                                                    
                                                        <li><a target="_blank" rel="noopener" href="https://www.zhihu.com/people/fu-xi-ting-67-17-5"><i class="fa  fa-search"></i>知乎</a></li>
                                                    
                                                
                                                    
                                                        <li><a href="http://cuishikang.gitee.io/my_resume/#/"><i class="fa  fa-file-o"></i>简历</a></li>
                                                    
                                                
                                            </ul>
                                        </li>
                                    
                                
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <header id="kratos-mobile-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="color-logo"><a href="/">小崔的博客</a></div>
                    <div class="nav-toggle">
                        <a class="kratos-nav-toggle js-kratos-nav-toggle">
                            <i></i>
                        </a>
                    </div>
                </div>
            </header>
        </div>
        <div class="kratos-start kratos-hero-2">
            <!-- <div class="kratos-overlay"></div> -->
            <div class="kratos-cover kratos-cover-2 text-center">
                <div class="desc desc2 animate-box">
                    <a href="/">
                        <h2>小崔的博客</h2> <br />
                        <span></span>
                    </a>
                </div>
            </div>
        </div>

        <div id="kratos-blog-post">
            <div class="container">
                <div id="main" class="row">
                    

        

            <section class="col-md-8">

        

            <article>
    <div class="kratos-hentry kratos-post-inner clearfix">
        <header class="kratos-entry-header">
            
                <h1 class="kratos-entry-title text-center">axios封装通用的api接口方法</h1>
            
            
            <ul class="kratos-post-meta text-center">
                <li><i class="fa fa-calendar"></i> 2022-01-13</li>
                <li><i class="fa fa-user"></i> 作者 cuishikang</li>
                <li>
                    <i class="fa fa-edit"></i> 
                    
                    
                        ~9.15K
                    
                    字
                </li>
                
            </ul>
        </header>
        <div class="kratos-post-content">
            <div id="expire-alert" class="alert alert-warning hidden" role="alert">
                本文最后编辑于 <time datetime="1645266178553"></time> 前，其中的内容可能需要更新。
            </div>
            
            <hr />
            <p>::: tip 介绍<br>二次封装axios 实现api接口直接调用<br><br>:::</p>
<span id="more"></span>

<h2 id="封装通用的接口模块"><a href="#封装通用的接口模块" class="headerlink" title="封装通用的接口模块"></a>封装通用的接口模块</h2><p>安装axios <code>npm i axios -S</code></p>
<ul>
<li><p>路由配置分为三个阶段</p>
<ol>
<li>配置通用axios封装组件<ul>
<li>通常配置 <code>axios.create(&#123;&#125;)</code> 分支设置url基础地址 方便操控</li>
</ul>
</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 导入 axios组件</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"><span class="comment">// 设置基准axios路径 用常量保存</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> baseURL = <span class="string">&#x27;http://api-toutiao-web.itheima.net/app/&#x27;</span></span><br><span class="line"><span class="comment">// axios分支的方法 创建axios接口调用方法 取代单一的axios方法(方便单独设置)</span></span><br><span class="line"><span class="keyword">const</span> instance = axios.<span class="title function_">create</span>(&#123;</span><br><span class="line">  <span class="comment">// baseURL是axios属性 用来声明url基础路径(比对上面声明的常量)</span></span><br><span class="line">  <span class="attr">baseURL</span>: baseURL,</span><br><span class="line">  <span class="comment">// 设置响应超时 建议不设置 或者设置时间较长 会影响一些较大的文件下载</span></span><br><span class="line">  <span class="comment">// timeout: 10000</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><p>调用接口时候 调用一级接口组件 单独设置api接口组件</p>
</li>
<li><p>在需要调用接口的Vue文件 导入api接口</p>
</li>
</ol>
</li>
</ul>
<blockquote>
<p><big>一、</big>utils文件夹 一级路由 axios接口模块设置  <code>utils文件夹里面创建 request.js</code></p>
</blockquote>
<p><img src="https://jinyanlong-1305883696.cos.ap-hongkong.myqcloud.com/l4R3Z1oLAGnmx6X.png" alt="11"></p>
<ul>
<li>配置 <code>axios.create(&#123;&#125;)</code> 分支设置url基础地址 方便操控</li>
<li>调用axios方法 设置基本url路径 设置参数 配置数据<ul>
<li>设置属性:  method(请求方式) , url(地址) , data(请求体) , params(请求方式参数) , headers(请求头)<ul>
<li>data(请求体) , params(请求方式参数) 可以通过es6的动态属性名(键) 来实现动态切换</li>
</ul>
</li>
<li><font color =#ff3040>注意: 接口的url地址也是 http和https标准协议时候 那么axios基准路径不会拼接 会直接调用</font></li>
</ul>
</li>
<li>return 返回结果</li>
<li>设置axios的 请求拦截器<code>response.use()</code> 和 响应拦截器<code>response.use()</code></li>
<li><code>transformRequest</code>是axios请求处理(比拦截器还早) 通常会把数据转换为json格式 并且在headers(请求头)中做一些处理</li>
<li><code>paramsSerializer</code>是处理你向后端传递的数据 是一个负责向后端传递数据时序列化的函数 通常搭配qs把前端传递的数组序列化</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 导入qs序列化</span></span><br><span class="line"><span class="keyword">import</span> qs <span class="keyword">from</span> <span class="string">&#x27;qs&#x27;</span></span><br><span class="line"><span class="comment">//! qs序列化的用法</span></span><br><span class="line"> qs.<span class="title function_">stringify</span>(&#123;<span class="attr">ids</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]&#125;, &#123; <span class="attr">indices</span>: <span class="literal">false</span> &#125;)</span><br><span class="line"><span class="comment">// 转换形式： ids=1 ids=2 id=3</span></span><br><span class="line"> qs.<span class="title function_">stringify</span>(&#123;<span class="attr">ids</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]&#125;, &#123;<span class="attr">arrayFormat</span>: <span class="string">&#x27;indices&#x27;</span>&#125;)</span><br><span class="line"><span class="comment">// 转换形式： ids[0]=1 ids[1]=2 ids[2]=3</span></span><br><span class="line"> qs.<span class="title function_">stringify</span>(&#123;<span class="attr">ids</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]&#125;, &#123;<span class="attr">arrayFormat</span>: <span class="string">&#x27;brackets&#x27;</span>&#125;)</span><br><span class="line"><span class="comment">// 转换形式：ids[]=1 ids[]=2 ids[]=3</span></span><br><span class="line"> qs.<span class="title function_">stringify</span>(&#123;<span class="attr">ids</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]&#125;, &#123;<span class="attr">arrayFormat</span>: <span class="string">&#x27;repeat&#x27;</span>&#125;)</span><br><span class="line"><span class="comment">// 转换形式： ids=1 ids=2 id=3</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>目标: 封装通用的接口调用模块</p>
<p>作用: 配置通用的<code>axios</code>接口模块 方便统一修改 统一配置 统一更换 减低耦合</p>
<p><font color =#ff3040>注意: 封装分为 携带token请求头的通用封装 和 携带token还需要其他请求头参数的特殊封装</font></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 封装通用的接口调用模块</span></span><br><span class="line"><span class="comment">// 导入axios组件</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"><span class="comment">// 导入Vuex 实例化</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;@/store&#x27;</span></span><br><span class="line"><span class="comment">// 获取toekn</span></span><br><span class="line"><span class="keyword">import</span> &#123; getToken &#125; <span class="keyword">from</span> <span class="string">&#x27;@/utils/auth&#x27;</span></span><br><span class="line"><span class="comment">// 导入Vue router 实例化</span></span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">&#x27;@/router&#x27;</span></span><br><span class="line"><span class="comment">// 导入序列化函数</span></span><br><span class="line"><span class="keyword">import</span> qs <span class="keyword">from</span> <span class="string">&#x27;qs&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 请求的基准路径 常量保存并且支持导出</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> baseURL = <span class="string">&#x27;&#x27;</span></span><br><span class="line"><span class="comment">// 更规范的调用 通过process.env调用</span></span><br><span class="line"><span class="comment">// const baseURL = process.env.VUE_APP_BASE_API</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建独立的axios的实例</span></span><br><span class="line"><span class="keyword">const</span> instance = axios.<span class="title function_">create</span>(&#123;</span><br><span class="line">  <span class="comment">// baseURL是axios属性 用来声明url基础路径(比对上面声明的常量)</span></span><br><span class="line">  <span class="attr">baseURL</span>: baseURL</span><br><span class="line">  <span class="comment">// 设置响应超时 建议不设置 或者设置时间较长 会影响一些较大的文件下载</span></span><br><span class="line">  <span class="comment">// timeout: 5000</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 请求拦截器 (发送数据前的加工数据)</span></span><br><span class="line">instance.<span class="property">interceptors</span>.<span class="property">request</span>.<span class="title function_">use</span>(<span class="function">(<span class="params">config</span>) =&gt;</span> &#123; <span class="comment">// config是发送的数据</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(config)</span><br><span class="line">  <span class="comment">// 特殊处理headers传值（内容类型）</span></span><br><span class="line">  <span class="comment">// 判断Vuex中是否有token，如果有就添加到请求头</span></span><br><span class="line">  <span class="keyword">const</span> token = store.<span class="property">getters</span>.<span class="property">token</span></span><br><span class="line">  <span class="comment">// 如果存在token 把token存入headers请求头中</span></span><br><span class="line">  <span class="keyword">if</span> (token) &#123;</span><br><span class="line">    <span class="comment">//! config是发送的数据 headers是axios请求头</span></span><br><span class="line">    config.<span class="property">headers</span>[<span class="string">&#x27;token&#x27;</span>] = <span class="title function_">getToken</span>()</span><br><span class="line">    <span class="comment">// 也可以这样写</span></span><br><span class="line">    <span class="comment">// config.headers.token= getToken()</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 返回处理后的数据</span></span><br><span class="line">  <span class="keyword">return</span> config</span><br><span class="line">&#125;, <span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 如果请求拦截器错误 返回打印错误信息</span></span><br><span class="line">  <span class="keyword">return</span> <span class="title class_">Promise</span>.<span class="title function_">reject</span>(err)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 响应拦截器 (处理后端返回的数据)</span></span><br><span class="line">instance.<span class="property">interceptors</span>.<span class="property">response</span>.<span class="title function_">use</span>(<span class="function">(<span class="params">response</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 去除axios自带的一层data</span></span><br><span class="line">  <span class="keyword">return</span> response.<span class="property">data</span></span><br><span class="line">&#125;, <span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 处理token的过期操作</span></span><br><span class="line">  <span class="keyword">if</span> (err.<span class="property">response</span> &amp;&amp; err.<span class="property">response</span>.<span class="property">status</span> === <span class="number">401</span>) &#123;</span><br><span class="line">    <span class="comment">// ---------------------- 应该续签token 但是后端没做(按需设置)</span></span><br><span class="line">      </span><br><span class="line">    <span class="comment">// 进行清除操作</span></span><br><span class="line">      </span><br><span class="line">    <span class="comment">// 刷新跳转</span></span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">location</span>.<span class="property">href</span> = <span class="string">&#x27;/login&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 打印响应拦截器的错误信息</span></span><br><span class="line">  <span class="keyword">return</span> <span class="title class_">Promise</span>.<span class="title function_">reject</span>(err)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//! 这里是只携带token请求头的通用封装</span></span><br><span class="line"><span class="comment">// 封装一个通用的请求方法</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (options) =&gt; &#123;</span><br><span class="line">  <span class="comment">// 这里的返回值是Promise实例对象</span></span><br><span class="line">  <span class="comment">// return 返回数据 instance是声明的axios分支方法</span></span><br><span class="line">  <span class="keyword">return</span> <span class="title function_">instance</span>(&#123;</span><br><span class="line">    <span class="comment">// 如果没有传递请求方式，默认是使用get请求</span></span><br><span class="line">    <span class="attr">method</span>: options.<span class="property">method</span> || <span class="string">&#x27;GET&#x27;</span>,</span><br><span class="line">    <span class="comment">// 设置请求地址</span></span><br><span class="line">    <span class="attr">url</span>: options.<span class="property">url</span>,</span><br><span class="line">    <span class="comment">// 动态判断请求的方式(es6规则: 对象的键可以是动态的变量)</span></span><br><span class="line">    <span class="comment">// 如果不是data请求 那么就赋值params请求</span></span><br><span class="line">    <span class="comment">// api掉接口的时候 传递数据的属性名(键) 全部为data:&#123;&#125; (不管是那种方式)</span></span><br><span class="line">    [options.<span class="property">method</span>.<span class="title function_">toUpperCase</span>() === <span class="string">&#x27;GET&#x27;</span> ? <span class="string">&#x27;params&#x27;</span> : <span class="string">&#x27;data&#x27;</span>]: options.<span class="property">data</span>, <span class="comment">// toUpperCase转换为大写(请求为小写也可)</span></span><br><span class="line">    <span class="comment">// 设置请求头(一般用于跨域问题 和 传输token)</span></span><br><span class="line">   <span class="comment">// headers: options.headers // 不建议覆盖之前的headers 会出现问题 建议单独处理具备其他请求头的接口</span></span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//! 这里是不光携带token还需要其他请求头参数的特殊封装</span></span><br><span class="line"><span class="comment">// 名称要和api接口名称一致</span></span><br><span class="line"><span class="keyword">const</span> http = &#123;</span><br><span class="line">    </span><br><span class="line">  <span class="comment">//! 这里是特殊的post请求 通常需要携带参数</span></span><br><span class="line">  <span class="comment">// transformRequest是axios请求处理(拦截器还早) 允许在向服务器发送前，修改请求数据</span></span><br><span class="line">  <span class="comment">// 这里是只能提交json数据 需要在请求头数据类型中设置类型为json</span></span><br><span class="line">  postToken (url, params) &#123;</span><br><span class="line">    <span class="keyword">return</span> instance.<span class="title function_">post</span>(url, params, &#123;</span><br><span class="line">      <span class="attr">transformRequest</span>: [<span class="function">(<span class="params">params</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(params)</span><br><span class="line">      &#125;],</span><br><span class="line">      <span class="attr">headers</span>: &#123;</span><br><span class="line">        <span class="string">&#x27;Content-Type&#x27;</span>: <span class="string">&#x27;application/json&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">//! 这里是post请求不需要携带参数</span></span><br><span class="line">  <span class="comment">// 如果不需要参数 需要把参数2设置为null</span></span><br><span class="line">  postNo (url) &#123;</span><br><span class="line">    <span class="keyword">return</span> instance.<span class="title function_">post</span>(url, <span class="literal">null</span>, &#123;</span><br><span class="line">      <span class="attr">transformRequest</span>: [<span class="function">(<span class="params">params</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(params)</span><br><span class="line">      &#125;],</span><br><span class="line">      <span class="attr">headers</span>: &#123;</span><br><span class="line">        <span class="string">&#x27;Content-Type&#x27;</span>: <span class="string">&#x27;application/json&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">//! 这里是获取arraybuffer类型的二进制数据 适用于验证码图片的获取 需要指定responseType类型</span></span><br><span class="line">  <span class="comment">// 他没有没有参数 在post请求时候参数需要写null 因为其不携带参数 否则无法转换</span></span><br><span class="line">  getImage (url) &#123;</span><br><span class="line">    <span class="keyword">return</span> instance.<span class="title function_">post</span>(url, <span class="literal">null</span>, &#123;</span><br><span class="line">      <span class="attr">responseType</span>: <span class="string">&#x27;arraybuffer&#x27;</span></span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">//! 这里是特殊的get请求 通常特殊处理的get请求需要携带参数 比如通过qs来实现序列化的函数</span></span><br><span class="line">  <span class="comment">// paramsSerializer是处理你向后端传递的数据 是一个负责向后端传递数据时序列化的函数</span></span><br><span class="line">  qsToken (url, params) &#123;</span><br><span class="line">    <span class="keyword">return</span> instance.<span class="title function_">get</span>(url, &#123;</span><br><span class="line">      <span class="attr">params</span>: params,</span><br><span class="line">      <span class="attr">paramsSerializer</span>: <span class="function">(<span class="params">params</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">// 通过qs处理序列化函数</span></span><br><span class="line">        <span class="keyword">return</span> qs.<span class="title function_">stringify</span>(params, &#123; <span class="attr">arrayFormat</span>: <span class="string">&#x27;repeat&#x27;</span> &#125;)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//! qs序列化的用法</span></span><br><span class="line">  <span class="comment">// qs.stringify(&#123;ids: [1, 2, 3]&#125;, &#123; indices: false &#125;)</span></span><br><span class="line">  <span class="comment">// 转换形式： ids=1 ids=2 id=3</span></span><br><span class="line">  <span class="comment">// qs.stringify(&#123;ids: [1, 2, 3]&#125;, &#123;arrayFormat: &#x27;indices&#x27;&#125;)</span></span><br><span class="line">  <span class="comment">// 转换形式： ids[0]=1 ids[1]=2 ids[2]=3</span></span><br><span class="line">  <span class="comment">// qs.stringify(&#123;ids: [1, 2, 3]&#125;, &#123;arrayFormat: &#x27;brackets&#x27;&#125;)</span></span><br><span class="line">  <span class="comment">// 转换形式：ids[]=1 ids[]=2 ids[]=3</span></span><br><span class="line">  <span class="comment">// qs.stringify(&#123;ids: [1, 2, 3]&#125;, &#123;arrayFormat: &#x27;repeat&#x27;&#125;)</span></span><br><span class="line">  <span class="comment">// 转换形式： ids=1 ids=2 id=3</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 命名导出</span></span><br><span class="line"><span class="keyword">export</span> &#123; http &#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p><big>二、</big>api文件夹 二级路由 指定接口设置示例(登录二级接口) <code>api文件夹创建 二级路由模块(接口)</code></p>
</blockquote>
<p><img src="https://jinyanlong-1305883696.cos.ap-hongkong.myqcloud.com/rVzQ9DwZpHEboj6.png" alt="image-20210604084917295"></p>
<ul>
<li>设置命名导出(不是默认导出) 按需配置参数 </li>
<li><font color =#ff3040>注意: 接口的url地址也是 http和https标准协议时候 那么axios基准路径不会拼接 会直接调用</font> </li>
<li>return返回数据</li>
</ul>
<blockquote>
<p>目标: 封装单独的登录接口文件 </p>
<p>作用: 接口文件 单独控制 方便修改 降低耦合度 便于维护</p>
<p><font color =#ff3040>注意: 分为 携带token请求头的通用接口 和 携带token还需要其他请求头参数的特殊接口</font></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//! 这里是只携带token请求头通用接口</span></span><br><span class="line"><span class="keyword">import</span> request <span class="keyword">from</span> <span class="string">&#x27;@/utils/request&#x27;</span></span><br><span class="line"><span class="comment">//! 这里是不光携带token还需要其他参数的特殊接口</span></span><br><span class="line"><span class="keyword">import</span> &#123; http &#125; <span class="keyword">from</span> <span class="string">&#x27;@/utils/request&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//! 通用接口请求示例</span></span><br><span class="line"><span class="comment">// get请求</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">login</span> (data) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="title function_">request</span>(&#123;</span><br><span class="line">    <span class="attr">method</span>: <span class="string">&#x27;get&#x27;</span>,</span><br><span class="line">    <span class="attr">url</span>: <span class="string">&#x27;public/index.php/login/login/todata&#x27;</span>,</span><br><span class="line">    data</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// post请求</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">loginOut</span> (data) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="title function_">request</span>(&#123;</span><br><span class="line">    <span class="attr">method</span>: <span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">    <span class="attr">url</span>: <span class="string">&#x27;public/index.php/login/login/todata&#x27;</span>,</span><br><span class="line">    data</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//! 特殊接口的api封装 必须和一次封装名称一致</span></span><br><span class="line"><span class="comment">// 携带参数的post请求 </span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">postToken</span> (parm) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">await</span> http.<span class="title function_">postToken</span>(<span class="string">&#x27;public/index.php/custequip/authrule/index/index&#x27;</span>, parm)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 不需要携带参数的post请求</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">postNo</span> () &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">await</span> http.<span class="title function_">postNo</span>(<span class="string">&#x27;public/index.php/custequip/authrule/index/index&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 不需要携带参数arraybuffer类型二进制数据的post请求</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">getImage</span> () &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">await</span> http.<span class="title function_">getImage</span>(<span class="string">&#x27;public/index.php/custequip/authrule/index/index&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 序列化传参的get接口</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">qsToken</span> (parm) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">await</span> http.<span class="title function_">qsToken</span>(<span class="string">&#x27;public/index.php/custequip/authrule/index/index&#x27;</span>, parm)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p><big>三、</big>Vue文件引入二次封装的api接口</p>
</blockquote>
<ul>
<li>在页面调用二次封装的api接口</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; postToken, postNo, getImage, qsToken &#125; <span class="keyword">from</span> <span class="string">&#x27;@/api/user&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 通用接口请求我们就不操作了</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 特殊接口的api封装请求</span></span><br><span class="line"><span class="comment">// 携带参数的post请求</span></span><br><span class="line">    <span class="keyword">async</span> demo () &#123;</span><br><span class="line">      <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">postToken</span>(&#123;</span><br><span class="line">        <span class="comment">// 传递参数</span></span><br><span class="line">        <span class="attr">limt</span>: <span class="number">1</span></span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">// 不需要携带参数的post请求</span></span><br><span class="line">    <span class="keyword">async</span> demo () &#123;</span><br><span class="line">      <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">postNo</span>(&#123;</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">// 不需要携带参数的post的arraybuffer类型的二进制数据</span></span><br><span class="line">    <span class="keyword">async</span> demo () &#123;</span><br><span class="line">      <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">getImage</span>(&#123;</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">// 序列化传参的get接口</span></span><br><span class="line">    <span class="keyword">async</span> demo () &#123;</span><br><span class="line">      <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title function_">qsToken</span>(&#123;</span><br><span class="line">        <span class="attr">limt</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>] <span class="comment">// 序列化后 limt: 1 limt: 2 limt: 3</span></span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="关于基准路径的配置"><a href="#关于基准路径的配置" class="headerlink" title="关于基准路径的配置"></a>关于基准路径的配置</h2><blockquote>
<p>注意：<code>.env.development</code>和<code>.env.production</code> 文件用于配置开发环境和生产环境的基准url地址</p>
</blockquote>
<ul>
<li>.env.development 开发环境 (测试)</li>
<li>.env.production 生产环境 (正式)</li>
<li><code>#</code> 是注释的意思</li>
</ul>
<blockquote>
<p>.env.development 开发环境 配置文件 </p>
</blockquote>
<ul>
<li>如果只添加结尾数据 默认拼接localhost:</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># just a flag</span></span><br><span class="line">ENV = <span class="string">&#x27;development&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># base api</span></span><br><span class="line">VUE_APP_BASE_API = <span class="string">&#x27;https://miningrenewapi-qa.lshmnc.com.cn/&#x27;</span></span><br><span class="line">VUE_APP_BASE_API = <span class="string">&#x27;http://localhost:8089/&#x27;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>axios封装通用的接口模块 <code>utils文件夹 request.js</code></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建一个axios实例对象</span></span><br><span class="line"><span class="keyword">const</span> instance = axios.<span class="title function_">create</span>(&#123;</span><br><span class="line">  <span class="comment">// 基准路径 通过 开发环境 和 生产环境 文件进行配置(开发和生产单独设置 方便区分 脚手架方法需要重启)</span></span><br><span class="line">  <span class="attr">baseURL</span>: process.<span class="property">env</span>.<span class="property">VUE_APP_BASE_API</span>,</span><br><span class="line">  <span class="comment">// baseURL: baseURL,</span></span><br><span class="line">  <span class="comment">// 超时,如果超过10秒，后端没有返回数据，那么就报错</span></span><br><span class="line">  <span class="attr">timeout</span>: <span class="number">10000</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>总结：为了灵活配置接口的基准路径，可以基于配置文件的方式，在开发和生产环境分别切换到不同的地址。这种方式好处就是代码在开发和上线时修改比较容易</p>
</blockquote>

        </div>
        
            <div class="kratos-copyright text-center clearfix">
                <h5>本作品采用 <a rel="license nofollow" target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/">知识共享署名-相同方式共享 4.0 国际许可协议</a> 进行许可</h5>
            </div>
        
        <footer class="kratos-entry-footer clearfix">
            
                <div class="post-like-donate text-center clearfix" id="post-like-donate">
                
                
                    <a class="share" href="javascript:;"><i class="fa fa-share-alt"></i> 分享</a>
                    <div class="share-wrap" style="display: none;">
    <div class="share-group">
        <a href="javascript:;" class="share-plain qq" onclick="share('qq');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-qq"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain qzone" onclick="share('qzone');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-star"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weixin pop style-plain" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weixin"></i>
            </div>
            <div class="share-int">
                <div class="qrcode" id="wechat-qr"></div>
                <p>打开微信“扫一扫”，打开网页后点击屏幕右上角分享按钮</p>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weibo" onclick="share('weibo');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weibo"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain facebook style-plain" onclick="share('facebook');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-facebook"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain twitter style-plain" onclick="share('twitter');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-twitter"></i>
            </div>
        </a>
    </div>
    <script type="text/javascript">
        $(()=>{
            new QRCode("wechat-qr", {
                text: "https://cuishikang.gitee.io/2022/01/13/axios/1_axios_API/",
                width: 150,
                height: 150,
                correctLevel : QRCode.CorrectLevel.H
            });
        });
        function share(dest) {
            const qqBase        = "https://connect.qq.com/widget/shareqq/index.html?";
            const weiboBase     = "https://service.weibo.com/share/share.php?";
            const qzoneBase     = "https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?";
            const facebookBase  = "https://www.facebook.com/sharer/sharer.php?";
            const twitterBase   = "https://twitter.com/intent/tweet?";
            const hostUrl       = "https://cuishikang.gitee.io/2022/01/13/axios/1_axios_API/";
            const title         = "「axios封装通用的api接口方法」";
            const excerpt       = `::: tip 介绍二次封装axios 实现api接口直接调用:::`;
            let _URL;
            switch (dest) {
                case "qq"       : _URL = qqBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";     break;
                case "weibo"    : _URL = weiboBase+"url="+hostUrl+"&title="+title+excerpt;                                 break;
                case "qzone"    : _URL = qzoneBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";  break;
                case "facebook" : _URL = facebookBase+"u="+hostUrl;                                                        break;
                case "twitter"  : _URL = twitterBase+"text="+title+excerpt+"&url="+hostUrl;                                break;
            }
            window.open(_URL);
        };
    </script>
</div>
                
                </div>
            
            <div class="footer-tag clearfix">
                <div class="pull-left">
                <i class="fa fa-tags"></i>
                    <a class="tag-none-link" href="/tags/Vue/" rel="tag">Vue</a>, <a class="tag-none-link" href="/tags/axios/" rel="tag">axios</a>
                </div>
                <div class="pull-date">
                <span>最后编辑：2022-02-19</span>
                </div>
            </div>
        </footer>
    </div>
    
        <nav class="navigation post-navigation clearfix" role="navigation">
            
            <div class="nav-previous clearfix">
                <a title=" arraybuffer二进制图片转换" href="/2022/01/12/JavaScript/1.2_yan_zheng_ma/">&lt; 上一篇</a>
            </div>
            
            
            <div class="nav-next clearfix">
                <a title=" Vscode快速设置文档" href="/2022/01/14/Vscode/Vscode/">下一篇 &gt;</a>
            </div>
            
        </nav>
    
    
</article>

        

            </section>

        

                
            

<section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
    <!-- 文章和页面根据splitter来分割，没有的话就从头开始设置为sticky -->
    
    
                <aside id="krw-about" class="widget widget-kratos-about clearfix">
    <div class="photo-background"></div>
    <div class="photo-wrapper clearfix">
        <div class="photo-wrapper-tip text-center">
            <img class="about-photo" src="https://gitee.com/cuishikang/tmp/raw/master/img/20220220194141.png" />
        </div>
    </div>
    <div class="textwidget">
        <p class="text-center"></p>
    </div>
    <div class="site-meta">
        <a class="meta-item" href="/archives/">
            <span class="title">
                文章
            </span>
            <span class="count">
                91
            </span>
        </a>
        <a class="meta-item" href="/categories/">
            <span class="title">
                分类
            </span>
            <span class="count">
                13
            </span>
        </a>
        <a class="meta-item" href="/tags/">
            <span class="title">
                标签
            </span>
            <span class="count">
                33
            </span>
        </a>
    </div>
</aside>
            
                    <div class="sticky-area">
                
                
  <aside id="krw-categories" class="widget widget-kratos-categories clearfix">
    <h4 class="widget-title"><i class="fa fa-folder"></i>分类目录</h4>
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Css/">Css</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/">JavaScript</a><span class="category-list-count">15</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/RABC/">RABC</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vscode/">Vscode</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vue/">Vue</a><span class="category-list-count">13</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vue3/">Vue3</a><span class="category-list-count">25</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vue%E7%A7%BB%E5%8A%A8%E5%A4%B4%E6%9D%A1%E9%A1%B9%E7%9B%AE/">Vue移动头条项目</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/animation/">animation</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/axios/">axios</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/other/">other</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/uniapp/">uniapp</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/vue-element-admin/">vue-element-admin</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B0%8F%E7%A8%8B%E5%BA%8F/">小程序</a><span class="category-list-count">2</span></li></ul>
  </aside>


            
                
  <aside id="krw-tags" class="widget widget-kratos-tags clearfix">
    <h4 class="widget-title"><i class="fa fa-tags"></i>标签聚合</h4>
      <div class="tag-clouds">
        <a href="/tags/Css/" style="font-size: 0.66em;">Css</a> <a href="/tags/H5Video/" style="font-size: 0.6em;">H5Video</a> <a href="/tags/JavaScript/" style="font-size: 0.74em;">JavaScript</a> <a href="/tags/My-project/" style="font-size: 0.6em;">My project</a> <a href="/tags/QQ/" style="font-size: 0.6em;">QQ</a> <a href="/tags/RABC/" style="font-size: 0.6em;">RABC</a> <a href="/tags/Vant/" style="font-size: 0.71em;">Vant</a> <a href="/tags/VeeValidate/" style="font-size: 0.6em;">VeeValidate</a> <a href="/tags/Vscode/" style="font-size: 0.6em;">Vscode</a> <a href="/tags/Vue/" style="font-size: 0.8em;">Vue</a> <a href="/tags/Vue-Router/" style="font-size: 0.6em;">Vue Router</a> <a href="/tags/Vue3/" style="font-size: 0.77em;">Vue3</a> <a href="/tags/Vuex/" style="font-size: 0.6em;">Vuex</a> <a href="/tags/Vue%E7%A7%BB%E5%8A%A8%E7%AB%AF/" style="font-size: 0.74em;">Vue移动端</a> <a href="/tags/Windows/" style="font-size: 0.6em;">Windows</a> <a href="/tags/animation/" style="font-size: 0.63em;">animation</a> <a href="/tags/axios/" style="font-size: 0.69em;">axios</a> <a href="/tags/echarts/" style="font-size: 0.6em;">echarts</a>
      </div>
  </aside>

            
                
  <aside id="krw-posts" class="widget widget-kratos-posts">
  <h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
  <div class="tab-content">
      <ul class="list-group">
        
        
          
          
            <a class="list-group-item" href="/2111/02/19/JavaScript/5_algorithm_tree/"><i class="fa  fa-book"></i> JavaScript树形数据转换算法</a>
            
          
        
          
          
            <a class="list-group-item" href="/2099/01/27/Vue3/1.1.1_Vue_study/"><i class="fa  fa-book"></i> Vue3 学习手册(博客版)</a>
            
          
        
          
          
            <a class="list-group-item" href="/2022/02/28/JavaScript/0.2_scroll/"><i class="fa  fa-book"></i> JS 滚动到指定位置和回到顶部</a>
            
          
        
          
          
            <a class="list-group-item" href="/2022/02/23/Vue/5_Vue_nextTick/"><i class="fa  fa-book"></i> 理解$nextTick</a>
            
          
        
          
          
            <a class="list-group-item" href="/2022/02/20/hello-world/"><i class="fa  fa-book"></i> Hello World</a>
            
          
        
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
      </ul>
  </div>
  </aside>

            
    </div>
</section>
        
        </div>
    </div>
</div>
<footer>
    <div id="footer"  class="ap-lrc"  >
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 footer-list text-center">
                    <ul class="kratos-social-icons">
                        
                        
                        <li><a target="_blank" rel="nofollow" href="https://t.me/CandyUnion"><i class="fa fa-telegram"></i></a></li>
                        
                        
                        
                        <li><a target="_blank" rel="me" href="https://nya.one/@Candinya"><i class="fa fa fa-share-alt-square"></i></a></li>
                        <li><a target="_blank" rel="nofollow" href="https://github.com/Candinya"><i class="fa fa-github"></i></a></li>
                        
                    </ul>
                    <ul class="kratos-copyright">
                        <div>
                            <li>&copy; 2022 小崔的博客 版权所有.</li>
                            <li>本站已运行<span id="span_dt">Loading...</span></li>
                        </div>
                        <div>
                            <li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
                            <li>Site built with&nbsp;<i class="fa fa-heart throb" style="color:#d43f57"></i>&nbsp;by cuishikang.</li>
                        </div>
                        <div>
                            <li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
                            <li>Hosted on <a href="https://github.io" target="_blank">Github Pages</a></li>
                        </div>
                        <div>
                            
                            
                        </div>
                    </ul>
                </div>
            </div>
        </div>
        <div class="kr-tool text-center">
            <div class="tool">
                
                    <div class="box search-box">
                        <a href="/search/">
                            <span class="fa fa-search"></span>
                        </a>
                    </div>
                
                
                    <div class="box theme-box" id="darkmode-switch">
                        <span class="fa fa-adjust"></span>
                    </div>
                
                
            </div>
            <div class="box gotop-box">
                <span class="fa fa-chevron-up"></span>
            </div>
        </div>
    </div>
</footer>
</div>
</div>

        <script defer src="https://unpkg.com/bootstrap@3.3.4/dist/js/bootstrap.min.js"></script>
<script defer src="https://unpkg.com/nprogress@0.2.0/nprogress.js"></script>
<script>
    if (!window.kr) {
        window.kr = {};
    }
    window.kr.notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));
    window.kr.siteRoot = "/";
</script>


    <script async src="/js/candy.min.js"></script>



    <script defer src="https://unpkg.com/aplayer@1.10.1/dist/APlayer.min.js"></script>
    
    <script defer src="https://unpkg.com/meting@2/dist/Meting.min.js"></script>
    <meting-js
        server="netease"
        type="song"
        id="1375305989"
        order="random"
        fixed="true"
    >
    </meting-js>



    <script defer src="https://unpkg.com/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script defer src="https://unpkg.com/clipboard@2.0.6/dist/clipboard.min.js"></script>
<script defer src="/js/kratosr.min.js"></script>
<script defer src="/js/pjax.min.js"></script>


    <script defer src="/js/kr-dark.min.js"></script>



<!-- Extra support for third-party plguins  -->


    </body>
</html>